package com.kaiwukj.android.ufamily.mvp.ui.widget;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;

/**
 * Created by Administrator on 2019/10/25
 */
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int ORIENTATION = RecyclerView.VERTICAL;
    private int space;

    /**
     * @param space 单位dp
     */
    public SpaceItemDecoration(int space) {
        this.space = ConvertUtils.dp2px(space);
    }

    /**
     * @param space       单位dp
     * @param orientation 排列方向
     */
    public SpaceItemDecoration(int space, int orientation) {
        this.space = ConvertUtils.dp2px(space);
        this.ORIENTATION = orientation;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect,
                               @NonNull View view,
                               @NonNull RecyclerView parent,
                               @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            ORIENTATION = ((LinearLayoutManager) parent.getLayoutManager()).getOrientation();
            int pos = parent.getChildLayoutPosition(view);
            if (pos != 0) {
                if (ORIENTATION == LinearLayoutManager.VERTICAL) {
                    outRect.top = space;
                } else {
                    outRect.left = space;
                }
            }
        }

    }
}
